package com.TTBook.utils.security;

import org.springframework.security.providers.encoding.PasswordEncoder;

/**
 * @author Chris
 * Create On 2013-10-10 下午4:30:07 
 * <b>Class Description:</b><br>
 * TODO Please add your class description here
 */
public class Md5PwdEncoder implements PasswordEncoder {

	private final static MD5 m = new MD5();

	/**
	 * 加密密码。
	 */
	private String encodePassword(String rawPass) {
		if (rawPass == null) {
			return "";
		}
		return m.getMD5ofStr(rawPass.trim().toLowerCase());
	}

	/**
	 * 校验密码是否正确。
	 * 
	 * @param encPass 密文
	 * @param rawPass 明文
	 */
	private boolean isPasswordValid(String encPass, String rawPass) {
		String pass1 = "" + encPass;
		String pass2 = encodePassword(rawPass);
		return pass1.equals(pass2);
	}

    /* (non-Javadoc)
     * @see org.springframework.security.providers.encoding.PasswordEncoder#encodePassword(java.lang.String, java.lang.Object)
     */
    public String encodePassword(String rawPass, Object salt)  {
           return encodePassword(rawPass);
    }

    /* (non-Javadoc)
     * @see org.springframework.security.providers.encoding.PasswordEncoder#isPasswordValid(java.lang.String, java.lang.String, java.lang.Object)
     */
    public boolean isPasswordValid(String encPass, String rawPass, Object salt)  {
        return isPasswordValid(encPass,rawPass);
    }



}
